<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button v-if="~accessable.indexOf('provider:list_add')" class="filter-item" type="primary" v-waves icon="el-icon-plus" @click="handleUpdate(null)">添加</el-button>
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="请输入编码" v-model="filters.code"></el-input>
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="请输入简称" v-model="filters.like$shortname"></el-input>
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="请输入地址" v-model="filters.like$address"></el-input>
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="请输入电话" v-model="filters.phone"></el-input>
      
      <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" type="primary" v-if="~accessable.indexOf('provider:list_export')" :loading="isExportingExcel" v-waves icon="el-icon-document" @click="exportExcelFile">导出</el-button>
    </div>
    <el-table :height="tableHeight" :data="list" @sort-change="sortChange" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row
      style="width: 100%">
      <el-table-column align="center" type="index" label="序号" width="80">
      </el-table-column>

      <el-table-column prop="name" align="center" label="名称" width="120">
        <template slot-scope="scope">
          <span>{{scope.row.name}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="code" align="center" label="编码" width="120">
        <template slot-scope="scope">
          <span>{{scope.row.code}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="shortname" align="center" label="简称" width="120">
        <template slot-scope="scope">
          <span>{{scope.row.shortname}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="username" align="center" label="登录账号" width="120">
        <template slot-scope="scope">
          <span>{{scope.row.username}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="isAuthed" align="center" label="企业认证" width="120">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isAuthed>0?'success':'warning'">
            {{scope.row.isAuthed>0?'已认证':'未认证'}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="canRefundIn7days" align="center" label="7天无理由退换货" width="120">
        <template slot-scope="scope">
          <el-tag :type="scope.row.canRefundIn7days>0?'success':'warning'">
            {{scope.row.canRefundIn7days>0?'是':'否'}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="address" align="center" label="地址" width="120">
        <template slot-scope="scope">
          <span>{{scope.row.address}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="phone" align="center" label="电话" width="120">
        <template slot-scope="scope">
          <span>{{scope.row.phone}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="email" align="center" label="邮箱" width="120">
        <template slot-scope="scope">
          <span>{{scope.row.email}}</span>
        </template>
      </el-table-column>

      <el-table-column prop="state" align="center" label="状态" width="120">
        <template slot-scope="scope">
          <el-tag :type="scope.row.state>0?'success':'warning'">
            {{scope.row.state>0?'可用':'不可用'}}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="remark" align="center" label="备注" width="120">
        <template slot-scope="scope">
          <span>{{scope.row.remark}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="280" class-name="small-padding">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" v-if="~accessable.indexOf('provider:list_modify')" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="success" v-if="~accessable.indexOf('provider:list_rest_pwd')" @click="resetPwd(scope.row)">重置密码</el-button>
          <br>
          <br>
          <!-- <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button> -->
          <el-button size="mini" v-if="scope.row.state==1 && ~accessable.indexOf('provider:list_froze')" :loading="scope.row.isChangingState" type="danger" @click="stopUser(scope.row)">禁用</el-button>
          <el-button size="mini" v-if="scope.row.state==0 && ~accessable.indexOf('provider:list_active')" :loading="scope.row.isChangingState" type="primary" @click="startUser(scope.row)">启动</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="params.page"
        :page-sizes="[10,20,30,50]" :page-size="params.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <el-dialog :title="isEditing?'修改':'添加'" :visible.sync="dialogFormVisible">
      <el-form class="dialog-form" :rules="rules" ref="dataForm" :model="temp" label-position="left" label-width="130px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="temp.name" placeholder="请输入供应商名称"></el-input>
        </el-form-item>
          <el-form-item label="头像" prop="avatar">
          <el-upload class="avatar-uploader" :action="UPLOAD_URL" :show-file-list="false" :on-success="handleAvatarSuccess('avatar')"
            :before-upload="beforeAvatarUpload">
            <img v-if="temp.avatar" :src="temp.avatar|absUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>

        </el-form-item>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="编码" prop="code">
              <el-input v-model="temp.code" placeholder="请输入供应商编码"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="登录账号" prop="username">
              <el-input v-model="temp.username" placeholder="请输入登录账号"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="企业认证" prop="isAuthed">
              <el-radio-group v-model="temp.isAuthed">
                <el-radio :label="false">未认证</el-radio>
                <el-radio :label="true">已认证</el-radio>
              </el-radio-group>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="7天无理由退换货" prop="canRefundIn7days">
              <el-radio-group v-model="temp.canRefundIn7days">
                <el-radio :label="0">否</el-radio>
                <el-radio :label="1">是</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
         <el-row :gutter="20">
          <el-col :span="12">
        <el-form-item label="简称" prop="shortname">
          <el-input v-model="temp.shortname" placeholder="请输入简称"></el-input>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="电话" prop="phone">
          <el-input v-model="temp.phone" placeholder="请输入电话"></el-input>
        </el-form-item>
        </el-col>
         </el-row>
     <el-form-item label="邮箱" prop="email">
          <el-input v-model="temp.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>

        <el-form-item label="地址" prop="address">
          <el-input v-model="temp.address" placeholder="请输入地址"></el-input>
        </el-form-item>
        
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="temp.state">
            <el-radio :label="0">不可用</el-radio>
            <el-radio :label="1">可用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="temp.remark" placeholder="请输入备注"></el-input>
        </el-form-item>
       
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" :loading="submiting" @click="submitFormAction">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableHeight:400,
      isExportingExcel: false,
      total:0,
      temp: {
        id: "",
        username: "",
        name: "",
        isAuthed: "",
        address: "",
        shortname: "",
        phone: "",
        state: 1,
        remark: "",
        avatar: "",
        email: "",
        code: "",
        canRefundIn7days: ""
      },

      rules: {
        username: {
          required: true,
          message: "登录账号不能为空"
        },
        name: {
          required: true,
          message: "名称不能为空"
        },
        isAuthed: {
          required: true,
          message: "请指定是否企业认证"
        },
        address: {
          required: true,
          message: "地址不能为空"
        },
        shortname: {
          required: true,
          message: "简称不能为空"
        },
        phone: {
          required: true,
          message: "电话不能为空"
        },
        state: {
          required: true,
          message: "请指定账号状态"
        },
        email: {
          required: false,
          message: "邮箱不能为空"
        },
        code: {
          required: true,
          message: "编码不能为空"
        },
        canRefundIn7days: {
          required: true,
          message: "请指定是否可以7天无理由退换货"
        }
      }
    };
  },
  created() {
    this.init("provider", 1).loadData();
    this.tableHeight = window.innerHeight - 300;
  },
  filters: {},
  methods: {
    resetPwd(row){
        this.$confirm('确定重置密码吗?','提示').then(_=>{
          this.$post('/admin/provider/resetpwd',{
            id:row.id
          }).then(res=>{
             this.$$notify();
          });
        }).catch(_=>{

        });
    },
    stopUser(row){
      this.$confirm('确定禁用该用户吗？','提示').then(_=>{
          this.$set(row,'isChangingState',true);
          this.$post('/admin/provider/stop_provider',{
            id:row.id
          }).then(res=>{
             this.$set(row,'isChangingState',false);
             row.state = 0;
             this.$$notify();
          }).catch(_=>{
             this.$set(row,'isChangingState',false);
          });
      }).catch(_=>{
      });
    },
    startUser(row){
       this.$confirm('确定启用该用户吗？','提示').then(_=>{
          this.$set(row,'isChangingState',true);
          this.$post('/admin/provider/start_provider',{
            id:row.id
          }).then(res=>{
             this.$set(row,'isChangingState',false);
             row.state = 1;
             this.$$notify();
          }).catch(_=>{
             this.$set(row,'isChangingState',false);
          });
      }).catch(_=>{
      });
    },
    exportExcelFile() {
      this.isExportingExcel = true;
      this.exportExcel({
        title: "供应商列表",
        fields: [
          {
            field: "name",
            title: "供应商名称"
          },
          {
            field: "shortname",
            title: "简称"
          },
          {
            field: "code",
            title: "供应商编码"
          },
          {
            field: "username",
            title: "登录账号"
          },
          {
            field: "isAuthed",
            title: "企业认证",
            valueMap: ["未认证", "已认证"]
          },
          {
            field: "canRefundIn7days",
            title: "7天无理由退换货",
            valueMap: ["否", "是"]
          },

          {
            field: "phone",
            title: "电话"
          },

          {
            field: "state",
            title: "状态",
            valueMap: ["不可用", "可用"]
          },
          {
            field: "remark",
            title: "备注"
          },
          {
            field: "email",
            title: "邮件"
          }
        ]
      })
        .then(info => {
          this.isExportingExcel = false;
          this.openWin(info);
        })
        .catch(_ => {
          this.isExportingExcel = false;
        });
    }
  }
};
</script>
    